package com.soma.game.controller;

import com.google.common.collect.Lists;
import com.soma.game.model.RankUserVo;
import com.soma.game.model.ResEnv;
import com.soma.game.service.GameScoreService;
import com.soma.game.service.GameStaticService;
import com.soma.game.service.soma.SomaService;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.exception.ExceptionUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @program: soma-game
 * @description: 排名服务
 * @author: hduprince
 * @create: 2018-07-18 14:20
 **/
@RestController
@RequestMapping("/rank")
public class RankController {

    Logger logger = LoggerFactory.getLogger(RankController.class);

    @Resource
    private SomaService somaService;

    @Resource
    private GameStaticService gameStaticService;

    @Resource
    private GameScoreService gameScoreService;

    @RequestMapping("list")
    public ResEnv list(@RequestParam("userId") Long uid,
                       @RequestParam("gameId") Long gameId,
                       @RequestParam(value = "type", required = false, defaultValue = "record") String type,
                       @RequestParam(value = "sessionId", required = false) Long sessionId,
                       @RequestParam(value = "sessionType", required = false) String sessionType
                       ){
        try {
logger.info("uid : " + uid);
logger.info("gameId : " + gameId);
logger.info("type : " + type);
logger.info("sessionId : " + sessionId);
logger.info("sessionType : " + sessionType);


            Date start = new Date();
logger.info("RankController start : " + start);

            List<Long> uidList = null;

            switch (sessionType) {
                case "0":
                    uidList = Lists.newArrayList(uid,sessionId);
                    break;
                case "1":
                    logger.info("sessionId : " +sessionId);
                    logger.info("start : " + new Date());
                    uidList = somaService.getGroupUsers(sessionId);
                    logger.info("end : " + new Date());
                    break;
                case "2":
                    logger.info("uid : " +uid);
                    logger.info("start : " + new Date());
                    uidList = somaService.getUserFriends(uid);
                    uidList.add(uid);
                    logger.info("end : " + new Date());
                    break;

            }

            List<RankUserVo> list = null;

            if( StringUtils.equals(type, "record")){
                list = gameScoreService.getRankUserVos(gameId, uidList, uid);
            } else {
                list = gameStaticService.getRankUserVos(gameId, uidList, uid);
            }



            ResEnv result = ResEnv.success(list);

            RankUserVo current = list.stream().filter(RankUserVo::getSelf).findFirst().orElse(null);

            if(current != null){
                result.addOption("current", current);

                if(current.getRank()-2 >= 0){
                    RankUserVo prev = list.get(current.getRank()-2);
                    result.addOption("prev", prev);
                }

                if(current.getRank() < list.size()) {
                    RankUserVo next = list.get(current.getRank());
                    result.addOption("next", next);
                }
            }
logger.info("RankController end : " + new Date());
logger.info("RankController time : " + (new Date().getTime() - start.getTime()));
            return result;
        } catch (Exception e){
            logger.error("操作失败， {}", ExceptionUtils.getStackTrace(e));
            return ResEnv.fail("操作失败, " + e.getMessage());
        }
    }
}
